package com.fyx.controller;

import com.fyx.annotation.Log;
import com.fyx.entity.CheckListEntity;
import com.fyx.service.ICheckService;
import com.fyx.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = "审核模块")
@RestController
@RequestMapping("/api/check")
@RequiredArgsConstructor
public class CheckController {
    @Autowired
    private ICheckService iCheckService;

    @Log("查询发放审核")
    @ApiOperation("查询发放审核")
    @GetMapping(value ="toCheck")
    @PreAuthorize("@el.check('toCheck:list')")
    public ResponseEntity<Object> query(CheckListEntity checkListEntity, Pageable pageable){
        Map mapResult = iCheckService.toCheck(checkListEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("查询客服-文字邮件审核")
    @ApiOperation("查询客服-文字邮件审核")
    @GetMapping(value="toCheck1")
    @PreAuthorize("@el.check('toCheck1:list')")
    public ResponseEntity<Object> query1(CheckListEntity checkListEntity, Pageable pageable){
        checkListEntity.setMail_type(2);
        checkListEntity.setMail_stype(3);
        Map mapResult = iCheckService.toCheck(checkListEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Log("查询我的审核")
    @ApiOperation("查询我的审核")
    @GetMapping(value = "myCheckList")
    @PreAuthorize("@el.check('myCheck:list')")
    public ResponseEntity<Object> myCheckList(CheckListEntity checkListEntity, Pageable pageable){
        Map mapResult = iCheckService.myCheckList(checkListEntity,pageable);
        return new ResponseEntity<>(PageUtil.toPage(mapResult.get("list"),mapResult.get("total")), HttpStatus.OK);
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    @Log("批量审核通过")
    @ApiOperation("批量审核通过")
    @PostMapping(value = "checkAllPass")
    @PreAuthorize("@el.check('toCheck:checkAllPass')")
    public ResponseEntity<Object> checkAllPass(@Validated @RequestBody CheckListEntity checkListEntity) throws IOException {
        String[] ids =checkListEntity.getIds().split(",");
        for(int i=0;i<ids.length;i++){
            Criteria criteria =new Criteria();
            criteria.and("_id").is(ids[i]);
            criteria.orOperator(Criteria.where("status").is("0"),Criteria.where("status").exists(false));
            if(ids[i]!=null&&!ids[i].equals("")) {
                CheckListEntity checkListEntity1 = mongoTemplate.findOne(new Query().addCriteria(criteria), CheckListEntity.class, "check_list");
            if(checkListEntity1!=null){
                checkListEntity1.setCheck_reason(checkListEntity.getCheck_reason());
                iCheckService.checkPass(checkListEntity1);
            }
            }
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    @Log("审核通过")
    @ApiOperation("审核通过")
    @PostMapping(value = "checkPass")
    @PreAuthorize("@el.check('toCheck:checkPass')")
    public ResponseEntity<Object> checkPass(@Validated @RequestBody CheckListEntity checkListEntity) throws IOException {
        if(checkListEntity.getUserid()!=null){
            iCheckService.checkPass(checkListEntity);
        }else{
            iCheckService.checkAllPass(checkListEntity);
        }

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("审核不通过")
    @ApiOperation("审核不通过")
    @PostMapping(value = "checkNotPass")
    @PreAuthorize("@el.check('toCheck:checkNotPass')")
    public ResponseEntity<Object> checkNotPass(@Validated @RequestBody CheckListEntity checkListEntity) throws IOException {
        iCheckService.checkNotPass(checkListEntity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    @Log("修改邮件")
    @ApiOperation("修改邮件")
    @PutMapping(value = "editCheckInfo")
    @PreAuthorize("@el.check('sendEmail:edit')")
    public ResponseEntity<Object> editCheckInfo(@Validated @RequestBody CheckListEntity checkListEntity) throws IOException {
        iCheckService.editCheckInfo(checkListEntity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("撤销审核")
    @ApiOperation("撤销审核")
    @PostMapping(value = "checkCancel")
    @PreAuthorize("@el.check('myCheck:checkCancel')")
    public ResponseEntity<Object> checkCancel(@Validated @RequestBody CheckListEntity checkListEntity) throws IOException {
        iCheckService.checkCancel(checkListEntity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }




    @Log("补发关注礼包")
    @ApiOperation("补发关注礼包")
    @PostMapping(value = "bufaGuanzhuGift")
    @PreAuthorize("@el.check('toCheck:bufaGuanzhuGift')")
    public ResponseEntity<Object> bufaGuanzhuGift() throws IOException {
        iCheckService.bufaGuanzhuGift();
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
